package com.codeoss.framework.view.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.data.metadata.Table;
import com.codeoss.framework.data.store.ITableStore;
import com.codeoss.framework.service.impl.BaseService;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.util.Utils;
import com.codeoss.framework.view.component.Component;
import com.codeoss.framework.view.component.LinkRecord;
import com.codeoss.framework.view.component.Page;
import com.codeoss.framework.view.component.store.IComponentStore;

@Service("linkrecordComponentService")
public class LinkRecordComponentService extends ComponentService {
	@Autowired
	private IComponentStore componentStore;
	@Autowired
    private ITableStore tableStore;
	@Autowired
	private Utils utils;
	public long getCount(YunContext yunContext, Component component) {
		if(yunContext.getCurrentId()==null) {
			return 0;
		}
		LinkRecord linkRecord=(LinkRecord)component;
		String refPageCode=linkRecord.getRefPageCode();
		if(!StringUtils.isEmpty(refPageCode)) {
			Page page=componentStore.get(refPageCode);
			String tableName=page.getDataSourceConfigList().get(0).getTableName();
			Table table = tableStore.get(tableName);
			BaseService baseService = utils.getBaseService(table);
			QueryWrapper<Object> query=new QueryWrapper<Object>();
			query.eq(yunContext.getMasterModel()+"_id",yunContext.getCurrentId());
			long count=baseService.count(query);
			return count;
		}
		
		return 0;
	}
}
